package org.csu.mypetstore.persistence.impl;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.persistence.CartDao;
import org.csu.mypetstore.persistence.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {

    private static final String GET_ITEM_BY_USERID = "select * from cart1 where accountName = ?";
    private static final String GET_ITEM_BY_USERID_ITEMID = "select * from cart1 where accountName = ? && itemid = ?";
    private static final String UPDATE_ITEM_BY_USERID_ITEMID = "update cart1 set num = num+1 where accountName = ? && itemid = ?";
    private static final String DELETE_NUM_BY_USERID_ITEMID = "update cart1 set num = num-1 where accountName = ? && itemid =?";
    private static final String DELETE_ITEM_BY_USERID_ITEMID = "delete from cart1 where accountName = ? && itemid = ?";
    private static final String INSERT_INTO_CART = "insert into cart1 (accountName,itemid,num) values (?,?,?)";
    private static final String DELETE_ITEM_BY_USERID = "delete from cart1 where accountName = ? ";

    @Override
    public List<String> getItems(Account account) {
        List<String> items = new ArrayList<>();
        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_ITEM_BY_USERID);
            preparedStatement.setString(1,account.getUsername());
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next())
            {
                int times = resultSet.getInt(3);
                for(int i = 1; i <= times ; i++)
                {
                    items.add(resultSet.getString(2));
                }
            }
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);

        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return items;
    }

    @Override
    public void insertCart(Account account, String itemId) {
        try {
            Connection connection = DBUtil.getConnection();
            if(isExit(account,itemId)==0)
            {
               PreparedStatement preparedStatement = connection.prepareStatement(INSERT_INTO_CART);
               preparedStatement.setString(1,account.getUsername());
               preparedStatement.setString(2,itemId);
               preparedStatement.setString(3,"1");
               preparedStatement.execute();
               DBUtil.closePreparedStatement(preparedStatement);
               DBUtil.closePreparedStatement(preparedStatement);
            }
            else
            {
                PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_ITEM_BY_USERID_ITEMID);
                preparedStatement.setString(1,account.getUsername());
                preparedStatement.setString(2,itemId);
                preparedStatement.execute();
                DBUtil.closePreparedStatement(preparedStatement);
                DBUtil.closePreparedStatement(preparedStatement);
            }


        }catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }

    public int isExit(Account account,String itemId) {
        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_ITEM_BY_USERID_ITEMID);
            preparedStatement.setString(1,account.getUsername());
            preparedStatement.setString(2,itemId);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next())
            {
                return resultSet.getInt(3);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    @Override
    public void deleteCart(Account account, String itemId) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(DELETE_ITEM_BY_USERID_ITEMID);
            preparedStatement.setString(1,account.getUsername());
            preparedStatement.setString(2,itemId);
            preparedStatement.execute();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closePreparedStatement(preparedStatement);

        }catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }

    @Override
    public void clearUpCart(Account account) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(DELETE_ITEM_BY_USERID);
            preparedStatement.setString(1,account.getUsername());
            preparedStatement.execute();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closePreparedStatement(preparedStatement);

        }catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }

    @Override
    public void minusCart(Account account, String itemId) {
        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(DELETE_NUM_BY_USERID_ITEMID);
            preparedStatement.setString(1,account.getUsername());
            preparedStatement.setString(2,itemId);
            preparedStatement.execute();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closePreparedStatement(preparedStatement);
            int is = isExit(account,itemId);
            System.out.println(is);
            if( is == 0)
            {
                deleteCart(account,itemId);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
